# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=oneTBB
pkgbase=mingw-w64-tbb
pkgname=("${MINGW_PACKAGE_PREFIX}-tbb")
pkgver=2021.4.0
pkgrel=3
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://software.intel.com/en-us/oneapi/onetbb'
license=("custom")
pkgdesc='oneAPI Threading Building Blocks (mingw-w64)'
conflicts=("${MINGW_PACKAGE_PREFIX}-intel-tbb")
#replaces=("${MINGW_PACKAGE_PREFIX}-intel-tbb")
#provides=("${MINGW_PACKAGE_PREFIX}-intel-tbb")
depends=("${MINGW_PACKAGE_PREFIX}-hwloc")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://github.com/oneapi-src/${_realname}/archive/v${pkgver}.tar.gz"
        001-fix-build-with-mingw.patch
        002-do-not-copy-lib.patch
        003-fix-win32-defs.patch)
sha256sums=('021796c7845e155e616f5ecda16daa606ebb4c6f90b996e5c08aebab7a8d3de3'
            '1e342350d8655a7930a4ba7e28f654296cdd8f99233c957398f86d66f8a31e3e'
            '01aa1f98b28874070e24df93ff766d980d66bc17a31e63d42f0e3e8645c972cd'
            'cd533959fcabe4c4f5452ec349cb25f048b96c8b8e5c560f786553daefd19390')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  # https://github.com/oneapi-src/oneTBB/pull/618 for both 001 and 003
  patch -p1 -i ${srcdir}/001-fix-build-with-mingw.patch
  patch -p1 -i ${srcdir}/003-fix-win32-defs.patch

  patch -p1 -i ${srcdir}/002-do-not-copy-lib.patch
}

build() {
  cd ${srcdir}
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  if check_option "debug" "n"; then
    _build_type="Release"
  else
    _build_type="Debug"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -GNinja \
    -DCMAKE_BUILD_TYPE=${_build_type} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DTBB_TEST=OFF \
    -DTBB_STRICT=OFF \
    -DBUILD_SHARED_LIBS=ON \
    ../${_realname}-${pkgver}

  cmake --build .
}

package() {
  DESTDIR=${pkgdir} cmake --install build-${MSYSTEM}
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
